﻿using System;
using System.Linq;
using System.Web;
using Guan.CMS.CommandProcessor.Command;
using Guan.CMS.Data.Infrastructure;
using Guan.CMS.Data.Repositories;
using Guan.CMS.Domain.Commands.SiteConfig;

namespace Guan.CMS.Domain.Handlers.SiteConfig
{
    public class UpdateAboutUsPageHandler : ICommandHandler<UpdateAboutUsPageCommand>
    {
        private readonly ISiteConfigRepository _siteConfigRepository;
        private readonly IUnitOfWork _unitOfWork;

        public UpdateAboutUsPageHandler(
            ISiteConfigRepository siteConfigRepository,
                                         IUnitOfWork unitOfWork)
        {
            this._siteConfigRepository = siteConfigRepository;
            this._unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(UpdateAboutUsPageCommand command)
        {
            var model = _siteConfigRepository.GetList().FirstOrDefault();
            var isAdd = model == null;
            if (isAdd)
            {
                model = new Model.Entities.SiteConfig();
            }
            model.AboutUs = HttpUtility.HtmlDecode(command.Content);
            if (isAdd)
            {
                _siteConfigRepository.Add(model);
            }
            else
            {
                _siteConfigRepository.Update(model);
            }
            _unitOfWork.Commit();
            return new CommandResult(true);
        }
    }
}